import requests
import re
import json


def get_lol_images():
    # 获取昵称 和皮肤代码
    url = "https://lol.qq.com/biz/hero/champion.js"
    jscode = requests.get(url).text

    regex = r'"keys":(.*?),"data"'
    list_js = re.findall(regex, jscode)

    # 将字符串转为字典
    dict_js = json.loads(list_js[0])

    # 生成英雄 皮肤图片地址
    pic_list = []
    for hero_id in dict_js:
        for i in range(20):
            i = str(i)
            if len(i) == 1:
                hero_num = "00" + i
            elif len(i) == 2:
                hero_num = "0" + i

            hero_nums = hero_id + hero_num
            hero_url = 'https://game.gtimg.cn/images/lol/act/img/skin/big' + hero_nums + '.jpg'
            pic_list.append(hero_url)

    list_filepath = []
    path = r"F:\crawlers\python3\requests\lolimg\\"  
    #存放图片的绝对路径
    for name in dict_js.values():
        for i in range(20):
            filepath = path + name + str(i) + '.jpg'
            list_filepath.append(filepath)

    #保存图片
    n = 0
    for pic_url in pic_list:
        res = requests.get(pic_url)
        n += 1
        if res.status_code == 200:
            print('正在下载:' + list_filepath[n])

            with open(list_filepath[n], 'wb') as f:
                f.write(res.content)


get_lol_images()
